package com.personal.gateway.custom.customException;

import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import com.personal.gateway.utils.StringTemplateUtils;

import java.util.HashMap;

/**
 * @author BigHead
 * @date 2022/1/13 11:15
 * @description ConfigurationOptionCheckException
 */
public class ConfigurationOptionCheckException extends RuntimeException {
    private static final String RETURN_MESSAGE =
            "必须输入: ${NECESSARY} => [目标类型: ${TARGET_TYPE}; 现有输入值: ${EXTANT_VALUE}]";
    private static final String NECESSARY = "NECESSARY";
    private static final String TARGET_TYPE = "TARGET_TYPE";
    private static final String EXTANT_VALUE = "EXTANT_VALUE";

    private final String extantValue;
    private final ConfigurationOption<?, ?> configurationOption;

    public ConfigurationOptionCheckException(String extantValue, ConfigurationOption<?, ?> configurationOption) {
        this.extantValue = extantValue;
        this.configurationOption = configurationOption;
    }

    @Override
    public String getMessage() {
        return StringTemplateUtils.render(
                RETURN_MESSAGE,
                new HashMap<String, String>(3) {{
                    put(NECESSARY, String.valueOf(configurationOption.isNecessary()));
                    put(TARGET_TYPE, configurationOption.getConfigurationType().getTypeString());
                    put(EXTANT_VALUE, null != extantValue ?
                            ("".equals(extantValue) ? "空字符串" : extantValue) :
                            "null"
                    );
                }}
        );
    }
}
